<?php

include_once "../config.php";
include_once "../util.php";

connect_db();

$flashback = isset($_GET['fb']) ? $_GET['fb'] : 10;

function get_account_num() {
  $today = date('Y-m-d');
  $sql = "select count(*) from user_statistic where collect_date = '$today'";
  $ret = sql_or_die($sql);
  $row = mysql_fetch_row($ret);
  return $row[0];
}

function get_all_proxy_num() {
  $sql = "select count(*) from proxy";
  $ret = sql_or_die($sql);
  $row = mysql_fetch_row($ret);
  return $row[0];
}

function get_valid_proxy_num() {
  // Get max fail rate for proxy
  $sql = "select * from core_config where name = 'valid_proxy_fail_rate'";
  $ret = sql_or_die($sql);
  $row = mysql_fetch_assoc($ret);
  $max_rate = $row['value'];

  // Get proxy's tryout count
  $sql = "select * from core_config where name = 'proxy_tryout_count'";
  $ret = sql_or_die($sql);
  $row = mysql_fetch_assoc($ret);
  $tryout = $row['value'];

  // Get all proxy number
  $sql = "select count(*) from proxy";
  $ret = sql_or_die($sql);
  $row = mysql_fetch_row($ret);
  $count = $row[0];

  // Get proxies have been used today and are invalid
  $today = date('Y-m-d');
  $sql = "select count(*) from proxy, proxy_log where proxy.addr = proxy_log.proxy_ip and proxy_log.collect_date = '$today' and 
(proxy_log.use_count >= $tryout and proxy_log.fail_count / proxy_log.use_count >= $max_rate) ";
  $ret = sql_or_die($sql);
  $row = mysql_fetch_row($ret);
  $count -= $row[0];

  return $count;
}

function get_data($date) {
  $sql = sprintf("select count(distinct(user)) as account, sum(followed_count) as followed, sum(follow_count) as following, sum(tweet_count) as tweet
from user_statistic 
where collect_date = '%s'",
                 $date);
  $ret = mysql_query($sql);
  if (!$ret) {
    die("SQL:($sql) failed:" . mysql_error());
  }
  if (mysql_num_rows($ret) != 1) {
    die("SQL:($sql) returned 0 row");
  }
  $data = mysql_fetch_assoc($ret);

  return $data;
}

function get_delta($cur, $pre, $key) {
  if ($pre == '')
    return '';
  $delta = $cur[$key] - $pre[$key];
  if ($delta > 0) {
    $delta = "<span class='incr'>↑" . $delta . "</span>";
  }
  else if ($delta < 0) {
    $delta = "<span class='decr'>↓" . $delta . "</span>";
  }
  else {
    $delta = '';
  }
  return $delta;
}

function fill_row($data, $date) {
  $cur_data = $data[$date];
  $pre = date('Y-m-d', strtotime('-1 day', strtotime($date)));

  $pre_data = isset($data[$pre]) ? $data[$pre] : '';

  $row = '';
  // Date
  $row .= "<td>$date</td>";

  // Followed By
  $delta = get_delta($cur_data, $pre_data, "followed");
  $row .= "<td>". $cur_data['followed'] . "$delta</td>";
  // Following
  $delta = get_delta($cur_data, $pre_data, "following");
  $row .= "<td>". $cur_data['following'] . "$delta</td>";
  // Tweet
  $delta = get_delta($cur_data, $pre_data, "tweet");
  $row .= "<td>". $cur_data['tweet'] . "$delta</td>";
  // Account number
  $delta = get_delta($cur_data, $pre_data, "account");
  $row .= "<td>". $cur_data['account'] . "$delta</td>";
  
  echo "<tr>$row<tr/>";
}


?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" type="text/css" href="/lib/css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/aggregate.css">
    <title>每日汇总信息 | taras运行状态</title>
  </head>
  <body>
  <?php  echo "<center>" . date('Y-m-d H:i:s') . "</center>"; ?>
  <?php 
    $all_proxy_num = get_all_proxy_num();
    $valid_proxy_num = get_valid_proxy_num();
    $account_num = get_account_num();
    $proxy_load = NULL;
    $proxy_needed = sprintf("%.0f", ceil($account_num / 50));

    if ($proxy_needed > $valid_proxy_num) {
      $proxy_load = "<span style='background-color:#FFFF66; color: $cdcdcd; margin-left:10px'> Need " . ($proxy_needed - $valid_proxy_num) . " more proxy!!!</span>";
    }
  ?>
<?php  echo "<center>account: {$account_num}, proxy: {$proxy_needed} / {$valid_proxy_num} {$proxy_load}"?>
    <table id="aggre-table" cellspacing="0">
      <tr>
        <td>Date</td>
        <td>Followed By</td>
        <td>Following</td>
        <td>Tweet</td>
        <td>Account Num</td>
      </tr>
      <?php
      $data = array();
      for ($i = $flashback - 1; $i >= 0; --$i) {
        $cur = date('Y-m-d', strtotime(sprintf('-%d day', $i)));
        $cur_data = get_data($cur);
        $data[$cur] = $cur_data;
      }
      
      for ($i = 0; $i < $flashback; ++$i) {
        $cur = date('Y-m-d', strtotime(sprintf('-%d day', $i)));
        fill_row($data, $cur);
      }
     ?>
    </table>
  </body>
</html>
